#!/bin/bash
# This script is used to run ut and st testcase.
# Copyright Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
set -eo pipefail

CUR_DIR=$(dirname $(readlink -f $0))
TOP_DIR=$(readlink -f ${CUR_DIR}/..)
TEST_DIR=${TOP_DIR}/"test"
SRC_DIR=${TOP_DIR}/"src"
ret=0

clean() {
  cd ${TEST_DIR}
  if [ -e ${TEST_DIR}/coverage.xml ]; then
    rm coverage.xml
    echo "remove last coverage.xml success"
  fi
  cd -
}

run_test_cpp() {
  echo "C++ tests are not implemented yet."
  # 待实现：编译并运行C++测试
  # build_cpp && run_cpp_tests
}

run_test_python() {
  python3 --version
  export PYTHONPATH="${TOP_DIR}:${PYTHONPATH}"
  python3 -m coverage run --branch --source ${TOP_DIR}/'flight_recorder_analyze' -m pytest ${TEST_DIR}/ut

  if [ $? -ne 0 ]; then
    echo "UT Failure"
    exit 1
  fi

  python3 -m coverage report -m
  python3 -m coverage xml -o ${TEST_DIR}/coverage.xml
}

run_test() {
  run_test_cpp
  run_test_python
}

main() {
  cd ${TEST_DIR}
  clean
  run_test
  echo "UT Success"
}

main